package com.joysuch.wwyt.api.base.controller;

import com.joysuch.wwyt.core.bean.BaseMenuFunctionDeleteBean;
import com.joysuch.wwyt.core.bean.BaseMenuFunctionEditBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.MenuTypes;
import com.joysuch.wwyt.core.service.BaseMenuFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/base/menu")
@Api(tags = "/base/menu-function 菜单")
public class MenuFunctionController {

    @Autowired
    private BaseMenuFunctionService baseMenuFunctionService;


    @RequestMapping("add")
    @ApiOperation("增加菜单")
    public Object add(@Valid @RequestBody BaseMenuFunctionEditBean entity) {
        return ResultBean.success(baseMenuFunctionService.addExMenu(entity));
    }

    @RequestMapping("update")
    @ApiOperation("修改菜单")
    public Object update(@Valid @RequestBody BaseMenuFunctionEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        return ResultBean.success(baseMenuFunctionService.updateExMenu(entity));
    }

    @RequestMapping("delete")
    @ApiOperation("删除菜单")
    public ResultBean delete(@Valid @RequestBody BaseMenuFunctionDeleteBean entity) {
        baseMenuFunctionService.deleteByExMenuId(entity.getExMenuId());
        return ResultBean.success("");
    }

    @GetMapping("folder-list")
    public Object folderList(Long tenantId, String orgCode) {
        List<SimpleSelectListBean> data = baseMenuFunctionService.parentList(MenuTypes.FOLDER.getCode(), null, tenantId, orgCode);
        return ResultBean.success(data);
    }


}
